import { createRouter, createWebHashHistory, createWebHistory } from "vue-router"
import { getToken } from "../utils/auth";

// 2. 定义路由配置
const routes = [
  {
    // path: "/",
    path: "/", component: () => import('../view/Home.vue'),
    redirect: '/user',
    children: [
      {
        path: "/user", component: () => import("../components/User.vue")
      },
      {
        path: "/class",  component: () => import("../components/Class.vue")
      },
      {
        path: "/course",  component: () => import("../components/Course.vue")
      },
      {
        path: "/student",  component: () => import("../components/Student.vue")
      },
      {
        path: "/score",  component: () => import("../components/Score/index.vue")
      }
    ]
  },
  // {
    
  //   redirect: '/user',
    
  // },
  // { path: "/login", component: () => import('../view/Login.vue') },
  { path: "/login", component: () => import('../view/img.vue') },


  // { path: "/langshanIndex", component: langshanIndex },
  // { path: "/screen", component: ScreenIndex },
];

// 3. 创建路由实例
const router = createRouter({
  // 4. 采用hash 模式
  history: createWebHashHistory(),
  // 采用 history 模式
  // history: createWebHistory(),
  routes, // short for `routes: routes`
});

router.beforeEach((to, from, next) => {
  console.log("to===", to);
  console.log("from===", from);
  const token = getToken("token")
  if (!token && to.path != '/login') {
    return next("/login")
  } else if (token && to.path == '/login') {
    return next('/')
  }
  next()
})
export default router

